package com.hejia.bulbul.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.entity.PopularizeCategory;
import com.hejia.alauda.service.PopularizeService;
import com.hejia.alauda.utils.HttpResponse;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.PagerHelper;

/**
 * 推广类别控制器
 * @author hyr
 *
 */
@Controller
@RequestMapping("popularizecategorys")
public class PopularizeCategoryController extends BaseController{

	@Resource
	private PopularizeService popularizeCategoryService;
	
	@RequestMapping()
	public String list(String name, Pager<PopularizeCategory> pager, ModelMap model){
		
		pager = this.popularizeCategoryService.findPopularizeCategorys(name, pager);
		
		PagerHelper<PopularizeCategory> helper = new PagerHelper<>();
        helper.setPager(pager);
        Map<String, String> map = new HashMap<>();
        if (name != null) {
            map.put("name", name);
        }
        helper.setParams(map);
        helper.setBaseUrl("/popularizecategorys");
        
        model.put("pagerHelper", helper);
		model.put("name", name);
		model.put("categorys", pager);
		
		return "popularizecategory/list";
	}
	
	@RequestMapping("add")
	public String add(ModelMap model){
		
		List<PopularizeCategory> biggers = this.popularizeCategoryService.findTopCategorys();
		model.put("biggers", biggers);
		
		return "popularizecategory/add";
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(PopularizeCategory category, ModelMap model){
		try{
			if(this.popularizeCategoryService.isExistName(category)){
				model.put("error", "已存在同名推广类别");
				List<PopularizeCategory> biggers = this.popularizeCategoryService.findTopCategorys();
				model.put("category", category);
				model.put("biggers", biggers);
				
				return "popularizecategory/add";
			}else{
				this.popularizeCategoryService.add(category);
				return "redirect:/popularizecategorys";
			}
		}catch(Exception e){
			model.put("error", "添加推广类别失败");
			List<PopularizeCategory> biggers = this.popularizeCategoryService.findTopCategorys();
			model.put("category", category);
			model.put("biggers", biggers);
			
			return "popularizecategory/add";
		}
	}
	
	@RequestMapping("edit/{id}")
	public String edit(@PathVariable Long id, ModelMap model){
		
		PopularizeCategory category = this.popularizeCategoryService.findCategoryById(id);
		List<PopularizeCategory> biggers = new ArrayList<PopularizeCategory>();
		if(category.getBigger() != null){
			biggers = this.popularizeCategoryService.findTopCategorys();
		}
		
		model.put("category", category);
		model.put("biggers", biggers);
		
		return "popularizecategory/edit";
	}
	
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public String edit(PopularizeCategory category, ModelMap model){
		
		try {
			if(this.popularizeCategoryService.isExistName(category)){
				List<PopularizeCategory> biggers = this.popularizeCategoryService.findTopCategorys();
				model.put("error", "已存在同名推广类别");
				model.put("category", category);
				model.put("biggers", biggers);
				
				return "popularizecategory/edit";
			}
			this.popularizeCategoryService.update(category);
			return "redirect:/popularizecategorys";
		} catch (Exception e) {
			List<PopularizeCategory> biggers = new ArrayList<PopularizeCategory>();
			if(category.getBigger() != null){
				biggers = this.popularizeCategoryService.findTopCategorys();
			}
			
			model.put("error", "更新推广类别失败");
			model.put("category", category);
			model.put("biggers", biggers);
			
			return "popularizecategory/edit";
		}
	}
	
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse delete(@PathVariable long id){
		HttpResponse response = new HttpResponse();
		try{
			if(this.popularizeCategoryService.isExistSmallerCategory(id)){
				response.setCode("400");
				response.setStatus("error");
				response.setMessage("存在稍小推广类别，不能删除");
			}else if(this.popularizeCategoryService.isUsedByPopularize(id)){
				response.setCode("400");
				response.setStatus("error");
				response.setMessage("已被推广使用，不能删除");
			}else{
				this.popularizeCategoryService.deletePopularizeCategory(id);
				
				response.setCode("201");
				response.setStatus("success");
				response.setMessage("删除推广类别成功");
			}
			
		}catch(Exception e){
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("删除推广类别失败 " + e.getMessage());
		}
		return response;
	}
}
